package com.yonyou.gmmc.service.pointactivity.util;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;


public class StringUtils {
	private static Logger logger = Logger.getLogger(StringUtils.class);
	public static String conCateNatePhoneNum(String telpre,String tel){
		String returnStr = "";
		if(telpre != null && !"".equalsIgnoreCase(telpre) && !"null".equalsIgnoreCase(telpre)){
			returnStr = telpre+"-";
		}
		if(tel != null && !"".equalsIgnoreCase(tel) && !"null".equalsIgnoreCase(tel)){
			returnStr = returnStr + tel;
		}
		return returnStr;
	}
	
	/**
	 * @param str
	 * @return val=null or val = "" return true
	 * added by wangwenhu 2010-03-18
	 */
	public static boolean notNull(String str){
		return !(str== null || "".equals(str.trim()));
	}
	public static boolean isNull(String str){
		return (str== null || "".equals(str.trim()));
	}
	
	/**
	 * @author  zhangxianchao
	 * 使用 sign 链接两个字符串
	 * for example:
	 * concatenateWithSign("ABC","DEF","/") 
	 * return "ABC/DEF"
	 * 页面如果需要换行，可以sign 传入 "<BR>"
	 * 
	 * @param str1
	 * @param str2
	 * @param sign
	 * @return
	 */
	public static String concatenateWithSign(String str1,String str2,String sign){
		String returnStr = "";
		if(str1 != null && !"".equalsIgnoreCase(str1) && str2 != null && !"".equalsIgnoreCase(str2)){
			returnStr = (str1+sign+str2);
		}else{
			if(str1 != null && !"".equalsIgnoreCase(str1)){
				returnStr = (str1);
			}
			if(str2 != null && !"".equalsIgnoreCase(str2)){
				returnStr = (str2);
			}
		}
		return returnStr;
	}
	/**
	 * 
	 * 功能说明              :
	 * 创建人                   : zhangxianchao
	 * 最后修改日期    : 2011-5-19
	 * @param douStr
	 * @return
	 */
	public static String convertDoubleStr(String douStr){
		if(douStr!=null&&douStr.substring(0,1).equals(".")){
			return "0"+douStr;
		}else{
			return douStr;
		}
	}
	
	/**
	 * @function:将String类型的数值转换成 乘以 一定比率的 Double类型
	 * @param str
	 * @param ratio
	 * @return
	 * @author  zhangxianchao
	 */
	public static Double switchDubFromStr(String str,double ratio){
		double dubStr = 0.00;
		BigDecimal bd = null;
		if(str != null && !"".equalsIgnoreCase(str)){
			dubStr = Double.parseDouble(str);
			dubStr = Double.parseDouble(StringUtils.mul(dubStr, ratio));
			bd = new BigDecimal(dubStr+"");
			bd = bd.setScale(4);
			return bd.doubleValue();
		}else{
			return dubStr;
		}	
	}
	/**
	 * @function:将Double类型的数值转换成 乘以 一定比率的 String类型
	 * @param sub
	 * @param ratio
	 * @return
	 * @author  zhangxianchao
	 */
	public static String switchStrFromDub(Double sub,double ratio){
		String str = "0";
		if(sub != null){
			str = StringUtils.mul(sub.doubleValue(),ratio);
//			System.out.println("chuanru"+sub.doubleValue());
//			System.out.println("xishu====="+ratio);
//			System.out.println("xishu2===="+new BigDecimal(ratio).setScale(4,2));
//			System.out.println("xishu3===="+new BigDecimal(ratio).setScale(4,2).doubleValue());
//			BigDecimal bd = new BigDecimal(subStr+"");
//			bd = bd.setScale(4,2);
//			str = bd.doubleValue() + "";
			
		}
		return StringUtils.mulZero(str);
	}
	/**
	 * @function:将Integer类型的数值转换成 乘以 一定比率的 String类型
	 * @param sub
	 * @param ratio
	 * @return
	 * @author  zhangxianchao
	 */
	public static String switchStrFromInt(Double sub,int ratio){
		String str = "0";
		if(sub != null){
			int subStr = sub.intValue() * ratio;
			str = subStr + "";
		}
		return str;
	}
	
	/**
	 * double 乘法
	 */
	public static String mul(double d1,double d2){
		BigDecimal bd1 = new BigDecimal(Double.toString(d1)).setScale(4,2);
		BigDecimal bd2 = new BigDecimal(Double.toString(d2)).setScale(4,2);
		return bd1.multiply(bd2).setScale(4,2).toString();
	}
	public static String mulZero(String str){
		String returnStr = "";
		boolean flag = false;
		if(str != ""){
			returnStr = str;
			int pointAt = returnStr.length();
			if(returnStr.indexOf(".") != -1){
				pointAt = returnStr.indexOf(".");
			}
			for(int i = returnStr.length()-1; i >= 0 ; i--){
				if((i > pointAt-1 )&&( "0".equalsIgnoreCase(returnStr.charAt(i)+"") || ".".equalsIgnoreCase(returnStr.charAt(i)+""))){
					returnStr = returnStr.substring(0,i);
				}else{
					break;
				}
			}
		}
		return returnStr;
	}
	
	/**
	 * 
	 * @param str1
	 * @param str2
	 * @param flag 是否以百分比输出
	 * @return
	 * str1 / str2
	 */
	public static String div(String str1,String str2,boolean flag){
		String returnStr = "";
		if(str1 != null && str2 != null && !"".equalsIgnoreCase(str1) && !"".equalsIgnoreCase(str2)){
			if(Double.parseDouble(str2) != 0){
				BigDecimal bd1 = new BigDecimal(str1);
				BigDecimal bd2 = new BigDecimal(str2);
				//System.out.println("1============="+bd1.toString());
				//System.out.println("1============="+bd2.toString());
				//System.out.println("res1============="+bd1.divide(bd2,4,BigDecimal.ROUND_HALF_UP).toString());
				//System.out.println("res2============="+bd1.divide(bd2,4,BigDecimal.ROUND_HALF_UP).setScale(4,2).toString());
				returnStr = bd1.divide(bd2,4,BigDecimal.ROUND_HALF_UP).setScale(4,2).toString();
				returnStr = StringUtils.mulZero(returnStr);
				if(flag){
					BigDecimal returnBD = new BigDecimal(returnStr).setScale(2,BigDecimal.ROUND_HALF_UP);
					//System.out.println("res2============="+returnBD.toString());
					returnStr = StringUtils.mulZero((returnBD.multiply(new BigDecimal("100"))).toString()) + "%"; 
				}
			}
		}
		return returnStr;
		
	}
	
	public static boolean isEmpty(String str) {
		return null == str || "".equals(str.trim()) || "undefined".equals(str.trim()) || "UNDEFINED".equals(str.trim()) || "null".equals(str.trim());
	}
	/**
	 * 
	 * 功能说明              :获得首字母大写
	 * 创建人                   : zhangxianchao
	 * 最后修改日期    : 2010-9-27
	 * @param str
	 * @return
	 */
	public static String getFirstUpper(String str){
		str = str.substring(0,1).toUpperCase()+str.substring(1);
		return str;
	}

	
	public static String convertMatchStr(String str) {
		//other choice
		str = str.replace('*', '%');
		str = str.replace('?', '_');
		//-end--
		return "%" + str + "%";		
	}
	
	public static String alignString(String str, int length, char c) {
		int tLen = length;
		if(str != null) {
			tLen = str.length();
		}
		StringBuilder builder = new StringBuilder();
		for(;tLen<length;tLen++) {
			builder.append(c);
		}
		builder.append(str);
		return builder.toString();
	}
	
	/**
	 * 功能说明：判断数组中是否有重复的数据
	 * @param array
	 * @return
	 * added by qiuxianjie 2012-07-17
	 */
	public static boolean checkRepeat(String[] array) {
		Set<String> set = new HashSet<String>();
		for(String str:array) {
			set.add(str);
		}
		if(set.size() != array.length) {
			return false;  //有重复
		} else {
			return true;   //没有重复
		}
	}
	
}
